﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_quan_ly_loai_nha_hang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDataToGridview();
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (txtResTypeName.Text != "")
        {
            try {
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_AdResType";
                Dal.ParameterCollection = new string[1] { "@MoTa" };
                Dal.ValueCollection = new string[1] { txtResTypeName.Text };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
                lblInfo.Text = " Thêm mới thành công !";
                txtResTypeName.Text = "";
                LoadDataToGridview();
            }
            catch {
                lblInfo.Text = "Lỗi, vui lòng thực hiện lại";
            }
        }
    }
    protected void LoadDataToGridview()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetResType";
            DataTable db = Dal.GetDataTable();
            grvResType.DataSource = db;
            grvResType.DataBind();
        }
        catch {
            lblInfo.Text = "Lỗi Khi Load Dữ Liệu Vào GridView";
        }
    }
    protected void grvResType_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SQLHelper Dal = new SQLHelper();
        Dal.Open();
        int id = Convert.ToInt32(grvResType.DataKeys[e.RowIndex].Value);
        Dal.CommandText = "SP_DeleteResType";
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.ParameterCollection = new string[1] { "@LoaiNhahangID" };
        Dal.ValueCollection = new string[1] { Convert.ToString(id) };
        Dal.ExecuteNonQuery();
        Dal.Close();
        Dal.Dispose();
        LoadDataToGridview();
    }
    protected void grvResType_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvResType.EditIndex = e.NewEditIndex;
        LoadDataToGridview();
        ((TextBox)grvResType.Rows[e.NewEditIndex].Cells[0].Controls[0]).Enabled = true;
    }
    protected void grvResType_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SQLHelper Dal = new SQLHelper();
        Dal.Open();
        int id = Convert.ToInt32(grvResType.DataKeys[e.RowIndex].Value);
        string MoTa = ((TextBox)grvResType.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        Dal.CommandText = "SP_UpdateResType";
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.ParameterCollection = new string[2] { "@MoTa", "@LoaiNhaHangID" };
        Dal.ValueCollection = new string[2] { MoTa, Convert.ToString(id) };
        Dal.ExecuteNonQuery();
        grvResType.EditIndex = -1;
        Dal.Close();
        Dal.Dispose();
        LoadDataToGridview();
    }
    protected void grvResType_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvResType.EditIndex = -1;
        LoadDataToGridview();
    }
   
}
